﻿; (function ($) {
    var methods = {
        init: function (options) {
            var $this = $(this);
            var setting = {
                width: 260,
                height: 260,
                outputheight: 640,
                outputwidth: 640,
                Type: 0
            };

            setting = $.extend(setting, options);
            if (window["photoClip"] == undefined) {
                loadJs("/Scripts/jQuery-photoClip/js/iscroll-zoom.js", function () {
                    loadJs("/Scripts/jQuery-photoClip/js/hammer.js", function () {
                        loadJs("/Scripts/jQuery-photoClip/js/lrz.all.bundle.js", function () {
                            loadJs("/Scripts/jQuery-photoClip/js/jquery.photoClip.js", function () {
                                InitHtml();
                            });
                        });
                    });
                });
            } else {
                InitHtml();
            }
            function InitHtml() {
                window["photoClip"] = true;
                $this.each(function () {
                    default_place = $(this).attr("title") ? $(this).attr("title") : "";
                    var controlId = $(this).attr("id");
                    var control = $(this);
                    var stylestr = "";
                    switch (setting.Type) {
                        case 1: setting.width = 260; setting.height = 260; break;
                        case 2: setting.width = 520; setting.outputwidth = 1280; stylestr = 'style="border-radius:0%;width:480px;height:240px"'; break;
                        default: setting.width = 260; setting.height = 260; break;
                    }

                    $(this).html(' <div class="userInfo"><div class="hd-avatar"><a href="javascript:;" ' + stylestr + ' class="userPic" id="' + controlId + 'image-view"><span >编辑头像</span><input type="file" class="upload-file" id="' + controlId + 'upload-file" accept="image/*"><input type="hidden" id="' + controlId + 'hidmyface" /></a></div></div>');
                    if ($('#' + controlId + 'article').length == 0)
                        $(this).parents("body").append('<article id="' + controlId + 'article" style="z-index:1000000000" class="zzsc-container"><div id="' + controlId + 'clipArea" class="clipArea"></div><div class="images_action"><div class="new-contentarea"><button type="button" class="btn btn-outline btn-default btn-lg" id="' + controlId + 'cancelBtn">取 消</button></div><div class="pull-right"><button type="button" class="btn btn-outline btn-default btn-lg" id="' + controlId + 'clipBtn">选 取</button></div></div></article>');
                    var clipArea = new bjj.PhotoClip("#" + controlId + "clipArea", {
                        size: [setting.width, setting.height],
                        outputSize: [setting.outputwidth, setting.outputheight],
                        file: "#" + controlId + "upload-file",
                        view: "#" + controlId + "image-view",
                        ok: "#" + controlId + "clipBtn",
                        loadStart: function () {
                            console.log("照片读取中");
                        },
                        loadComplete: function () {
                            console.log("照片读取完成");
                            $('#' + controlId + 'article').show();
                        },
                        clipFinish: function (dataURL) {
                            $(control).find("#" + controlId + "hidmyface").val(dataURL);
                            $('#' + controlId + 'article').hide();
                        }
                    });
                    //取消
                    $("#" + controlId + "cancelBtn").click(function () {
                        $('#' + controlId + 'article').hide();
                    });
                    if ($(control).attr("img") != "") {
                        $(control).find("#" + controlId + "hidmyface").val($(control).attr("img"));
                        $(control).find("#" + controlId + "image-view").css('background', 'url(' + $(control).attr("img") + ') center center / contain no-repeat');
                        $(control).attr("img", "");
                    }
                });
            }

        },
        set: function (myface) {
            var controlId = $(this).attr("id");
            var control = $(this);

            $(control).find("#" + controlId + "hidmyface").val(myface);
            if (myface == '' || myface == null) {
                $(control).find("#" + controlId + "image-view").css('background', 'url(/Content/img/DefaultAvatar.png) center center / contain no-repeat');
            }
            else {
                if ($(control).find("#" + controlId + "hidmyface").length == 0) {
                    $(control).attr("img", myface);
                } else {
                    $(control).find("#" + controlId + "image-view").css('background', 'url(' + myface + ') center center / contain no-repeat');
                }
            }
        },
        get: function () {
            var controlId = $(this).attr("id");
            var control = $(this);
            return $(control).find("#" + controlId + "hidmyface").val();
        },
        readonly: function () {
            var controlId = $(this).attr("id");
            var control = $(this);
            $(control).find("#" + controlId + "image-view").attr("disabled", "disabled");
        }
    }

    $.fn.FaceControl = function (method) {
        if (window != window.parent && typeof (window.parent.$("#").FaceControl) == "function") {
            if (arguments.length > 1) {

                return window.parent.$(this).FaceControl(method, arguments[1]);
            } else {
                return window.parent.$(this).FaceControl(method);
            }
        } else {
            if (methods[method]) {
                return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
            } else if (typeof method === 'object' || !method) {
                return methods.init.apply(this, arguments);
            } else {
                $.error('Method' + method + 'does not exist');
            }
        }
    };

})($);